<?php 
//扩展 - 短信函数库	加载：load("@.sms")

//------------------------------------------天翼平台--------------------------------------------------

function get_AccessToken($app_id,$app_secret,$grant_type='client_credentials',$state='1',$scope='') {
	$url='https://oauth.api.189.cn/emp/oauth2/v2/access_token';
	
	$param['app_id']=$app_id;
	$param['app_secret']=$app_secret;
	$param['grant_type']=$grant_type;
	$str = "app_id=$app_id&app_secret=$app_secret&grant_type=$grant_type";
    $result = curl_post($url,$str);
    
    $resultArray = json_decode($result,true);
    return $resultArray;
}



function get_Token($app_id,$app_secret) {
	$url='http://api.189.cn/v2/dm/randcode/token?';
	//获取 access_token
	$returnArray=get_AccessToken($app_id, $app_secret);
	if ($returnArray['res_code']!=0) {
		$returnArray['method']='get_Token';
		return $returnArray;
	}
	$access_token=$returnArray['access_token'];
	$timestamp = date('Y-m-d H:i:s');
	$_SESSION['sms_time']=time();
	$param['app_id']= "app_id=".$app_id;
    $param['access_token'] = "access_token=".$access_token;
    $param['timestamp'] = "timestamp=".$timestamp;
    ksort($param);
    $plaintext = implode("&",$param);
    $param['sign'] = "sign=".rawurlencode(base64_encode(hash_hmac("sha1", $plaintext, $app_secret, $raw_output=True)));
    ksort($param);
    $url .= implode("&",$param);
    $result = curl_get($url);
    $resultArray = json_decode($result,true);
    $data['token'] = $resultArray['token'];
    $data['access_token'] = $access_token;
    $data['timestamp'] = $timestamp;
    if ($resultArray['res_code']!=0) {
    	return false;
    }
	return $data;
}


function send_sms($app_id,$app_secret,$phone,$callback,$exp_time='5') {
	$url='http://api.189.cn/v2/dm/randcode/send';
	$returnArray=get_Token($app_id, $app_secret);
	if (!$returnArray) {
		return false;
	}
	$access_token=$returnArray['access_token'];
	$timestamp=$returnArray['timestamp'];
	$token=$returnArray['token'];
	$param['app_id']= "app_id=".$app_id;
    $param['access_token'] = "access_token=".$access_token;
    $param['timestamp'] = "timestamp=".$timestamp;
    $param['token'] = "token=".$token;
    $param['phone'] = "phone=".$phone;
    $param['url'] = "url=".$callback;
    if(isset($exp_time))
    	$param['exp_time'] = "exp_time=".$exp_time;
    ksort($param);
    $plaintext = implode("&",$param);
    $param['sign'] = "sign=".rawurlencode(base64_encode(hash_hmac("sha1", $plaintext, $app_secret, $raw_output=True)));
    ksort($param);
    $str = implode("&",$param);
    $result = curl_post($url,$str);
    
    $resultArray = json_decode($result,true);
    
    
    if ($resultArray['res_code']===0) {
    	return true;
    }else {
    	return $resultArray;
    }
    
}


//------------------------------------------------------资信通


//发送短信
function sendSms($phone,$content,$use='') {
	$time=time();
	$_SESSION['phone']=$phone;
	$p['OperID']='OperID=renrh';
	$p['OperPass']='OperPass=rrh13wlkj';
	$p['sendTime']='SendTime=';
	$p['validTime']='ValidTime='.$time+300;
	$p['AppendID']='AppendID=';
	$p['DesMobile']='DesMobile='.$phone;
	
	$sendUrl='http://221.179.180.158:9007/QxtSms/QxtFirewall?';
	
	$p['Content']='Content='.urlencode($content);
	$p['ContentType']='ContentType=15';
	$sendUrl.=implode('&', $p);
	$data=curl_get($sendUrl);
	$data=simplexml_load_string($data);
	$SmsLogModel=D('SmsLog');
	$log['phone']	= $phone;
	$log['content']	= iconv('gbk', 'utf-8', $content);
	$log['use']		= $use;
	$log['ip']		= get_client_ip();
	if ($data->code[0]=='03') {
		$log['status']		= 1;
		$SmsLogModel->add($log);
		return true;
	}else {
		$log['status']		= 0;
		$SmsLogModel->add($log);
		return false;
	}
}





//------------------------------------------系统调用---------------------------------

//发送微站管家四位验证码方法
function send_validateCode($phone='',$validCode='') {
	$time=time();
	//数据库查ip，防恶意短信
	$SmsLogModel=D('SmsLog');
	$smstime=$SmsLogModel->where("ip='%s' and `use`='validateCode'",get_client_ip())->order('create_time desc')->getField('create_time');
	if ($time-strtotime($smstime)<45) {
		$MZCode=140301; //请求过于频繁
		return $MZCode;
	}
	$_SESSION['sms_time']=$time;
	if (empty($phone)) {
		$phone=$_POST['phone'];
	}
	
	if (!regex_check($phone, '/^(130|131|132|133|134|135|136|137|138|139|147|150|151|152|153|155|156|157|158|159|180|181|182|183|184|185|186|187|188|189)\d{8}$/')) {
		$MZCode=140302; //手机格式不正确
		return $MZCode;
	}
	if (empty($validCode)) {
		$validCode=mt_rand(1000, 9999);
	}
	$_SESSION['validate_code']=$validCode;
	$content='【大悦盟】尊敬的大悦盟用户，'.$validCode.'是您本次的手机验证码，有效期为5分钟，请尽快验证。退定回复td';
	$content=iconv('utf-8', 'gbk', $content);
	$result=sendSms($phone,$content,'validateCode');
	if ($result) {
		$_SESSION['phone'] = $phone;
		$MZCode=0;
	}else {
		$MZCode=140303; //发送失败
	}
	return $MZCode;
	
}




?>